package View
{
	import laya.events.Event;
	import laya.ui.Image;
	import laya.utils.Ease;
	import laya.utils.Handler;
	import laya.utils.Tween;

	/**
	 * @author 刘吉
	 * @E-mail:liuji@mokylin.com
	 * 创建时间：2016-11-24 下午8:57:48
	 */
	
	public class Mole
	{
		private var stateNormal:Image;
		private var stateHit:Image;
		private var score:Image;
		private var downY:Number;
		private var upY:Number;
		private var type:Number;
		private var callback:Handler;
		
		private var isActive:Boolean;
		private var isShow:Boolean;
		private var isHit:Boolean;
		
		public function Mole(normal:Image, hit:Image, imgScore:Image, downY:Number, hitCallback:Handler)
		{
			stateNormal = normal;
			stateHit = hit;
			score = imgScore;
			this.downY = downY;
			this.upY = normal.y;
			callback = hitCallback;
			reset();
			
			this.stateNormal.on(Event.CLICK, this, this.hit);
		}
		
		private function reset():void
		{
			stateNormal.visible = false;
			stateHit.visible = false;
			score.visible = false;
			score.y = 27;
			
			isShow = false;
			isHit = false;
		}
		
		public function show():void
		{
			if(isShow)
			{
				return;
			}
			isShow = true;
			stateNormal.visible = true;
			stateNormal.y = downY;
			type = Math.random() < 0.5 ? 1 : 2;
			stateNormal.skin = "ui/mouse_normal_"+type+".png";
			stateHit.skin = "ui/mouse_hit_"+type+".png";
			score.skin = "ui/score_"+type+".png";
			Tween.to(stateNormal, {y:upY}, 500, Ease.backOut, Handler.create(this, showComple));
		}
		
		private function showComple():void
		{
			if(!isHit)
			{
				Tween.to(stateNormal, {y:downY}, 300, Ease.backIn, Handler.create(this, reset), 2000);
			}
		}
		
		private function hide():void
		{
		}
		
		private function hit():void
		{
			isHit = true;
			stateNormal.visible = false;
			stateHit.visible = true;
			Tween.clearTween(stateNormal);
			
			callback.runWith(type);
			
			score.visible = true;
			score.scale(0,0);
			Tween.to(score,{y:"-27", autoAlpha:0, scaleX:1, scaleY:1},300);
			
			Laya.timer.once(500, this, reset);
		}
	}
}